<?php

App::uses('AppController', 'Controller');

use logUtils\logUtils;

define('COMPROBAR_LOGIN',true);

class ControlController extends AppController {

	public $name = 'Control';
	var $loginUtils;
	var $logUtils;
	
	function beforeFilter(){
		parent::beforeFilter();
		$this->loginUtils = new loginUtils();
		$this->logUtils = new logUtils;
	}

	public function login() {
			$this->layout = 'login';
	}
	
	public function comprobar_permisos($controlador,$action){
		$url['controlador'] = $controlador;
		$url['action'] = $action;
		
		if($this->Session->read('sesion_activa') == false){
			$this->login_error();
		}
			
	}
	
	
	public function check_login(){
		$this->autoRender = false;
		
// 		$this->logUtils->borrar_fichero();
		$usuario = $_POST['usuario'];
		$clave = $_POST['clave'];
// 		print_r($this->Session->read());
		$comprobacion_login = $this->loginUtils->comprobar_login($usuario,$clave);

		if($comprobacion_login == true){
			$this->Session->write('sesion_activa',true);
			$this->login_ok();
		}
		if( $comprobacion_login == false ){ // LOGIN NO CORRECTO,YA SEA USUARIO INCORRECTO/NO EXISTE ETC.
			$this->login_error();
		}
		
	}
	
		
	public function login_error(){
		$this->redirect(array('controller' => 'control', 'action' => 'login')); //funciona
	}
	
	public function login_ok(){
		$this->redirect(array('controller' => 'portada', 'action' => 'index'));
 		//$this->redirect(array('controller' => 'portada', 'action' => 'index')); //funciona
	}
	
	public function cerrar_sesion(){
		$this->autoRender = false;
		$this->Session->delete('sesion_activa');
		$this->Session->delete('usuario');
		$data['redirect'] = 'ok';
		echo json_encode($data);
	}
	
	
	/*
	
	public function check_login(){
		$this->autoRender = false;
		$this->logUtils->borrar_fichero();
		$usuario = $_POST['usuario'];
		$clave = $_POST['clave'];
		$this->logUtils->crear_log("RECIBO AJAX usuario:".substr($usuario,0,10).'-Recibo clave:-'.substr($clave,0,10));
		if($this->loginUtils->comprobar_login($usuario,$clave) == true){
			$this->completar_sesion($usuario);
			$this->logUtils->crear_log("final");
			$this->login_ok();
		}else{
			$this->logUtils->crear_log("ERROR USUARIO");
			$this->login_error();
		}
	}
	
	
	public function completar_sesion($usuario){
		$this->logUtils->crear_log("Empezamos a crear sesion");
		
		$_SESSION['usuario'] = substr($usuario,0,10);
		$_SESSION['sesion_activa'] = '1';
		//$this->Session->write('usuario',substr($usuario,0,10));
		//$this->Session->write('sesion_activa', '1');
		$this->logUtils->crear_log("Hemos creado la sesion--Usuario:".$_SESSION['usuario']);
	}

	public function login_error(){
		$this->logUtils->crear_log("LOGIN ERROR,REDIRECION A LOGIN");
		$this->login();
	}
	
	public function login_ok(){
		$this->logUtils->crear_log("LOGIN OK , REDIRECIONAMOS A PORTADA");
// 		$this->redirect('/portada/index');
		header('Location: http://www.google.es');
		//$this->redirect('http://localhost/backend');
		$this->logUtils->crear_log("HEMOS PASADO LA REDIRECCION");
	}
	*/
}
